<template>
  <div class="artcile-list" >
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" :success-text="istext" success-duration='1500'>
<van-list
  v-model="loading"
  :finished="finished"
   :error.sync="error"
  error-text="请求失败，点击重新加载"
  finished-text="没有更多了"
  @load="onLoad"
>
  <ArtcileItem v-for="(obj,index) in list" :key="index" :datas="obj"></ArtcileItem>
</van-list>
</van-pull-refresh>

  </div>
</template>
<script>
import ArtcileItem from '@/components/article-item'
import { getArticles } from '@/api/user.js'
export default {
  name: 'ArticleList',
  components: {
    ArtcileItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: null,
      error: false,
      count: 0,
      isLoading: false,
      istext: ''
    }
  },
  methods: {
    async onLoad () {
      try {
        const res = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 1
        })
        console.log(res)
        this.list = [...this.list, ...res.data.data.results]
        this.loading = false
        if (res.data.data.results.length > 0) {
          this.timestamp = res.data.data.pre_timestamp
        } else {
          this.finished = true
        }
      } catch (err) {
        this.error = true
      }
    },
    async onRefresh () {
      try {
        const res = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 1
        })
        this.list.unshift(...res.data.data.results)
        this.isLoading = false
        this.istext = `刷新成功 更新了${res.data.data.results.length}条数据`
      } catch (err) {
        this.isLoading = false
        this.$toast('获取失败')
      }
    }
  }
}
</script>

<style saoped>
.artcile-list {
  height: 79vh;
  overflow: auto;
}
</style>
